package com.aiju.cash.ui.enter;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.utils.PriceUtil;
import com.aiju.utils.T;
import com.aiju.widgit.layout.SettingItemEditView;

/**
 * @author: 佐为
 * @date: 2017-05-26 09:47
 * @version: V1.0
 * @Description: 其他营业信息
 */
public class OtherManageActivity extends BaseTitleActivity implements View.OnClickListener {

    private EditText money;

    private String wifi = "F", parking = "F", no_smoking = "F", box = "F", avg_price = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_manage);
        setCenterTitle("其他营业信息");
        setRightTitle("确定");
        wifi = getIntent().getStringExtra("wifi");
        parking = getIntent().getStringExtra("parking");
        no_smoking = getIntent().getStringExtra("no_smoking");
        box = getIntent().getStringExtra("box");
        avg_price = getIntent().getStringExtra("avg_price");

        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                avg_price = money.getText().toString();
                if (TextUtils.isEmpty(avg_price)) {
                    T.ToastShort("请输入人均消费价格");
                    return;
                }
                Double avg = Double.valueOf(avg_price);
                if (1 > avg || avg > 99999) {
                    T.ToastShort("人均价格不能小于1元且不能超过99999元");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("wifi", wifi);
                intent.putExtra("parking", parking);
                intent.putExtra("no_smoking", no_smoking);
                intent.putExtra("box", box);
                intent.putExtra("avg_price", PriceUtil.formatDouble(avg_price));
                setResult(200, intent);
                finish();
            }
        });

        SettingItemEditView settingItemEditView = (SettingItemEditView) findViewById(R.id.set_other_money);
        settingItemEditView.setEditable(true);
        money = (EditText) settingItemEditView.getRightView();
        money.setHint("选填，请输入参考价格");
        money.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        money.setFilters(new InputFilter[]{
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                        String oriValue = dest.toString();
                        StringBuffer sb = new StringBuffer(oriValue);
                        sb.append(source);

                        String newValue = sb.toString();
                        Double aDouble = Double.valueOf(newValue);
                        if (aDouble > 200000) {
                            T.ToastShort("输入金额不能超过99999");
                            return "";
                        }
                        if (dest.toString().contains(".")) {
                            int index = dest.toString().indexOf(".");
                            int mlength = dest.toString().substring(index).length();
                            if (mlength == 3) {
                                return "";
                            }
                        }
                        return source;
                    }
                }
        });

        findViewById(R.id.set_other_time).setOnClickListener(this);
        findViewById(R.id.tv_other_parking).setOnClickListener(this);
        findViewById(R.id.tv_other_smoke).setOnClickListener(this);
        findViewById(R.id.tv_other_box).setOnClickListener(this);
        findViewById(R.id.tv_other_wifi).setOnClickListener(this);

        if (!TextUtils.isEmpty(avg_price) && Double.valueOf(avg_price) > 1) {
            money.setText(avg_price);
            money.setSelection(avg_price.length());
        }
        if (!TextUtils.isEmpty(wifi) && wifi.equals("T")) {
            findViewById(R.id.tv_other_wifi).setSelected(true);
        }
        if (!TextUtils.isEmpty(no_smoking) && no_smoking.equals("T")) {
            findViewById(R.id.tv_other_smoke).setSelected(true);
        }
        if (!TextUtils.isEmpty(box) && box.equals("T")) {
            findViewById(R.id.tv_other_box).setSelected(true);
        }
        if (!TextUtils.isEmpty(parking) && parking.equals("T")) {
            findViewById(R.id.tv_other_parking).setSelected(true);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.set_other_time:
                break;
            case R.id.tv_other_parking:
                v.setSelected(!v.isSelected());
                parking = v.isSelected() ? "T" : "F";
                break;
            case R.id.tv_other_smoke:
                v.setSelected(!v.isSelected());
                no_smoking = v.isSelected() ? "T" : "F";
                break;
            case R.id.tv_other_box:
                v.setSelected(!v.isSelected());
                box = v.isSelected() ? "T" : "F";
                break;
            case R.id.tv_other_wifi:
                v.setSelected(!v.isSelected());
                wifi = v.isSelected() ? "T" : "F";
                break;
        }
    }
}
